package com.yunxin.admin.system.application.impl;

import com.yunxin.admin.system.application.MenuApplicationService;
import com.yunxin.admin.system.application.assembler.MenuAssembler;
import com.yunxin.admin.system.application.command.AddMenuCommand;
import com.yunxin.admin.system.application.command.UpdateMenuCommand;
import com.yunxin.admin.system.domain.model.menu.Menu;
import com.yunxin.admin.system.domain.model.menu.MenuRepository;
import com.yunxin.admin.system.infra.common.constant.SystemConstants;
import com.yunxin.admin.system.infra.common.enums.MenuTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 菜单业务实现类
 *
 * @author zero°
 * @since 2023-05-22
 */
@Service
@RequiredArgsConstructor
public class MenuApplicationServiceImpl implements MenuApplicationService {

    private final MenuRepository menuRepository;

    private final MenuAssembler menuAssembler;
    @Override
    public boolean saveMenu(AddMenuCommand command) {
        Menu menu = menuAssembler.formAddMenuCommand(command);
        setComponent(menu, command.getType());
        String treePath = generateMenuTreePath(menu.getParentId());
        menu.setTreePath(treePath);
        return menuRepository.saveOrUpdate(menu);
    }

    @Override
    public boolean updateMenu(UpdateMenuCommand command) {
        Menu menu = menuAssembler.formUpdateMenuCommand(command);
        setComponent(menu, command.getType());
        String treePath = generateMenuTreePath(menu.getParentId());
        menu.setTreePath(treePath);
        return menuRepository.saveOrUpdate(menu);
    }


    @Override
    public boolean updateMenuVisible(Long menuId, Integer visible) {
       return menuRepository.updateVisible(menuId, visible);
    }



    @Override
    public boolean deleteMenu(Long id) {
        return menuRepository.remove(id);
    }

    /**
     * 获取component
     * @param entity entity
     * @param menuType menuType
     * @return component
     */
    private void setComponent(Menu entity, MenuTypeEnum menuType) {
        // 目录
        if (Objects.requireNonNull(menuType) == MenuTypeEnum.CATALOG) {
            if (entity.getParentId() == 0 && !entity.getPath().startsWith("/")) {
                // 一级目录需以 / 开头
                entity.setPath("/" + entity.getPath());
            }
            entity.setComponent("Layout");
            // 外链
        } else if (menuType == MenuTypeEnum.EXT_LINK) {
            entity.setComponent(null);
        }
    }

    /**
     * 部门路径生成
     *
     * @param parentId 父ID
     * @return 父节点路径以英文逗号(, )分割，eg: 1,2,3
     */
    private String generateMenuTreePath(Long parentId) {
        String treePath = null;
        if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
            treePath = String.valueOf(parentId);
        } else {
            Menu parent = menuRepository.getMenuById(parentId);
            if (parent != null) {
                treePath = parent.getTreePath() + "," + parent.getId();
            }
        }
        return treePath;
    }

}
